//
//  OfficialViewController.m
//  myMusicApp
//
//  Created by 11_wyq on 15/6/14.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "OfficialViewController.h"
#import "MainTabBarController.h"


@interface OfficialViewController ()


@end

@implementation OfficialViewController

- (void)dealloc
{
    [_myTableView release];
    [_array release];
    [super dealloc];
}

-(void)viewWillAppear:(BOOL)animated
{
     
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
 
    //       给导航栏上放自定义label
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH * 100, HEIGHT * 30)]autorelease];
    [view setBackgroundColor:[UIColor clearColor]];
    self.navigationItem.titleView = view;
    UILabel *title = [[[UILabel alloc]initWithFrame:CGRectMake(WIDTH * 30, 0, WIDTH * 50, HEIGHT * 30)]autorelease];
    title.text = @"首页";
    title.textColor = [UIColor whiteColor];
    [title setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];//加粗字体
    [view addSubview:title];
    
    
    self.view.backgroundColor = [UIColor blackColor];
    UIImage *image2 = [UIImage imageNamed:@"menu.png"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(locker)];
 
    //搜索
    UIImage *image3 = [UIImage imageNamed:@"Search@3x.png"];
    image3 = [image3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image3 style:UIBarButtonItemStylePlain target:self action:@selector(searchBar)];
    
    
    self.myTableView = [[[UITableView alloc] initWithFrame:CGRectMake(0,0,MYWIDTH ,HEIGHT * 603 )style:UITableViewStylePlain]autorelease];
    self.myTableView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.myTableView];
    
    
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    self.myTableView.showsVerticalScrollIndicator = NO;//取消纵向滑条
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消cell之间的线
    
    [self asker];
    
    
    
    
    NSUserDefaults *library = [NSUserDefaults standardUserDefaults];
    static BOOL number = 1;
    if ([library objectForKey:@"libraryNumber"] == nil) {
        
        [library setBool:number forKey:@"libraryNumber"];

    }else
    {
        [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
        [GiFHUD show];

    }
    
    
    

    
    
    [self addHeader];
}
//点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //监听网络
    if ([DataHandle shareHandleController].monitorNetwork == NO) {
        UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [aler show];
    }else
    {

        //数组保护
        if (self.array.count != 0) {
            
            OfficialModel *official = [self.array objectAtIndex:indexPath.row];
            VideoPlayerViewController *videoPlayerVC = [[[VideoPlayerViewController alloc] init]autorelease];
            videoPlayerVC.Id = official.modelId;//ID
            videoPlayerVC.type = official.type;//类型
            videoPlayerVC.url = official.url;//webView的网址
            videoPlayerVC.hidesBottomBarWhenPushed = YES;//隐藏下方Bar
//            NSLog(@"array = %@",self.array);
//            NSLog(@"type = %@",official.type);
            [self.navigationController pushViewController:videoPlayerVC animated:YES];
            
        }
    
    }
    
}
//cell高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (HEIGHT * 300);
}
//cell个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.array.count != 0) {
        return self.array.count;

    }else
    {
        return 10;
    }

}
//cell重用池
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *string = @"cell";
    OfficialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        cell = [[OfficialTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    

    cell.selectionStyle = UITableViewCellSelectionStyleNone;//取消点击效果
    
    
    
    //渐变效果
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.alpha = 0;
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1];
    cell.alpha = 1;
    [UIView commitAnimations];
    
    
    
    
    if (self.array.count != 0) {
        
        OfficialModel *official = [self.array objectAtIndex:indexPath.row];
        cell.official = [self.array objectAtIndex:indexPath.row];
        [[DataHandle shareHandleController] openDB];
        [[DataHandle shareHandleController] createCollectTable];
        NSMutableArray *array = [[DataHandle shareHandleController] selectCollect];
         
        if (array.count == 0) {
            cell.buer = NO;
     //       NSLog(@"array  没值");
        }else
        {
            for (CollectModel *collect in array) {

                NSString *modelId = [NSString stringWithFormat:@"%@", official.modelId];
                if ([collect.modelId isEqualToString:modelId]) {
                    cell.buer  = YES;
                    cell.official = [self.array objectAtIndex:indexPath.row];
           //         NSLog(@"判断 相同");
                    return cell;
                }
            }
            cell.buer = NO;
            cell.official = [self.array objectAtIndex:indexPath.row];
        //    NSLog(@"array 有值");
        }
    }
    
    
    
   
    
 
    
    return cell;
}
//网络数据读取
-(void)asker
{


    NSMutableDictionary *askerDic = [NSMutableDictionary dictionary];
    [askerDic setObject:@"0" forKey:@"D-A"];
    [askerDic setObject:@"0" forKey:@"offset"];
    [askerDic setObject:@"false" forKey:@"play"];
    [askerDic setObject:@"640*540" forKey:@"rn"];
    [askerDic setObject:@"10" forKey:@"size"];
    
    
    [Connect ConnectRequestAFWithURL:Officicl params:askerDic requestHeader:RequestHeader httpMethod:@"GET" block:^(NSObject *result) {

        if ([DataHandle shareHandleController].monitorNetwork == YES) {
            NSLog(@"有网");
            //需要判断请没请求下来数据
            if (result == nil) {
                [[DataHandle shareHandleController] openDB];
                self.array = [[DataHandle shareHandleController] selectOfficial];
            
                [self.myTableView.header endRefreshing];//结束下拉刷新
                
                UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
                [DataHandle shareHandleController].monitorNetwork = NO;
                [GiFHUD dismiss];//动态图隐藏
                [self.myTableView reloadData];
                [aler show];
            }else{
               
                self.array = [NSMutableArray array];
                [[DataHandle shareHandleController] openDB];
                [[DataHandle shareHandleController] dropOfficialTable];//删除表
                [[DataHandle shareHandleController] createOfficialTable];//创建表
                
                for (NSMutableDictionary *dic in (NSMutableArray *)result) {
                    OfficialModel *official = [[[OfficialModel alloc] init]autorelease];
                    official.title = [dic objectForKey:@"title"];
                    official.description_label = [dic objectForKey:@"description"];
                    official.posterPic = [dic objectForKey:@"posterPic"];
                    official.modelId = [dic objectForKey:@"id"];
                    official.type = [dic objectForKey:@"type"];
                    //  NSLog(@"type %@,%@",official.type,[dic objectForKey:@"subType"]);
                    official.url = [dic objectForKey:@"url"]; //webView的网址
                    [self.array addObject:official];
                    
                    [[DataHandle shareHandleController] insertOfficial:official];//插入数据
                    [DataHandle shareHandleController].monitorNetwork = YES;
                }
                
                // [self.MBP hide:YES];//菊花隐藏
                [self.myTableView reloadData];
                [self.myTableView.header endRefreshing];//结束下拉刷新
                
                [GiFHUD dismiss];//动态图隐藏
            }
        }else
        {
            
            [[DataHandle shareHandleController] openDB];
            self.array = [[DataHandle shareHandleController] selectOfficial];
           
            [self.myTableView reloadData];
            [self.myTableView.header endRefreshing];//结束下拉刷新
            [GiFHUD dismiss];//动态图隐藏
            UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
            [aler show];
        }
        
        
    }];
    
    
 
}

//下拉刷新
-(void)addHeader
{
    __block OfficialViewController *official=self;

    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{

        [official asker];
        
    }];
    
    //一进程序自动刷新
    //[self.myTableView.header beginRefreshing];
}

//搜索
-(void)searchBar
{
    SearchViewController *search = [[[SearchViewController alloc]init]autorelease];
    [GiFHUD dismiss];//动态图隐藏

    search.hidesBottomBarWhenPushed = YES;//隐藏Bar
    
    [self.navigationController pushViewController:search animated:YES];
    
}

//抽屉
-(void)locker
{
    [GiFHUD dismiss];//动态图隐藏 
    [self.sideMenuViewController presentLeftMenuViewController];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
